package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.dto.system.SysRoleDto;
import com.zhentao.entity.system.SysRole;
import com.zhentao.mapper.SysRoleMapper;
import com.zhentao.mapper.SysUserRoleMapper;
import com.zhentao.service.SysRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【sys_role(角色)】的数据库操作Service实现
* @createDate 2024-08-31 11:55:00
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{
    @Resource
    private SysRoleMapper sysRoleMapper ;
    @Resource
    private SysUserRoleMapper sysRoleUserMapper ;
    /**
     * 角色列表加分页
     * @param sysRoleDto
     * @return
     */
    @Override
    public Page<SysRole> findByPage(SysRoleDto sysRoleDto) {
        Page<SysRole> page = new Page<>(sysRoleDto.getPageNum(),sysRoleDto.getPageSize());
        Page<SysRole> pageInfo = sysRoleMapper.findByPage(sysRoleDto,page);
        return pageInfo;
    }
    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
    }

    /**
     * 修改角色
     * @param sysRole
     */
    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }

    /**
     * 删除角色
     * @param roleId
     */
    @Override
    public void deleteById(Long roleId) {
        sysRoleMapper.deleteById(roleId);
    }

    /**
     * 查询所有的角色
     * @return
     */
    @Override
    @Transactional
    public Map<String, Object> findAllRoles(Long userId) {
        // 查询当前登录用户的角色数据
        List<Long> sysUserRoles = sysRoleUserMapper.findSysUserRoleByUserId(userId);
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        // 构建响应结果数据
        Map<String , Object> resultMap = new HashMap<>() ;
        resultMap.put("allRolesList" , sysRoles) ;
        resultMap.put("sysUserRoles", sysUserRoles);
        return resultMap;
    }
}




